<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <!--{include file='comm/header.tpl'}-->
    <!--{if !isset($coupon_array)}-->
    <title>优惠券新增</title>
    <!--{else}-->
    <title>优惠券修改</title>
    <!--{/if}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--{include file='comm/comm_cal.tpl'}-->
    <!--引用div做成的下拉框效果样式-->
    <link rel="stylesheet" href="/quanliadmin/docroot/css/department_select.css" type="text/css" media="screen" />

    <!--引用div做成的下拉框效果js-->
    <script language="JavaScript" src="/quanliadmin/docroot/js/department_select.js" type="text/javascript"></script>
</head>
<body>
    <style>
        .imageSite{
            position: absolute;
            left:280px;
            top:8px;
        }
    </style>
	<div style="width:100%">

        <div class="container">

            <div class="comm_crub">
                <span class="now_crub">
                    正在操作：
                    <span id="title">
                    <!--{if !isset($coupon_array)}-->
                        优惠券新增
                    <!--{else}-->
                        优惠券修改
                    <!--{/if}-->
                    </span>
                </span>
            </div>
			
			<div class="items2">
				
                <div>
                    <form id="couponForm" class="form-horizontal" onsubmit="return false;">
                        
                        <div id="form_1">

                            <div class="form-group">
                                <label for="title" class="col-sm-3 control-label"><span class="red">*</span> 优惠券名称：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="couponName" name="couponName" placeholder="请输入优惠券名称【必填项】" value="<!--{$coupon_array.couponName|default:''}-->">
                                </div>
                                <input type="hidden" id="couponID" name="couponID" value="<!--{$coupon_array.couponID|default:0}-->">
                            </div>

                            <div class="form-group">
                                <div>
                                    <label class="col-sm-3 control-label"><span class="red">*</span> 优惠券使用日期：</label>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="startDate" name="startDate" readonly="" value="<!--{$coupon_array.startDate|default:''}-->">
                                    </div>
                                    <div class="fl">
                                        <label class="col-sm-1 control-label">～</label>
                                    </div>
                                    <div class="col-sm-3">
                                        <input type="text" class="form-control" id="endDate" name="endDate" readonly="" value="<!--{$coupon_array.endDate|default:''}-->">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="couponType" class="col-sm-3 control-label"><span class="red">*</span> 优惠券发放方式：</label>
                                <div class="col-sm-3">
                                    <select class="form-control" name="couponType" onchange="coupon.typeChange();">
                                        <option value="0">请选择</option>
                                        <option value="1"<!--{if isset($coupon_array) && $coupon_array.couponType == 1}--> selected="selected" <!--{/if}-->  >编号礼品卡</option>
                                        <option value="2"<!--{if isset($coupon_array) && $coupon_array.couponType == 2}--> selected="selected" <!--{/if}--> >账户礼品券</option>
                                        <option value="3"<!--{if isset($coupon_array) && $coupon_array.couponType == 3}--> selected="selected" <!--{/if}--> >万能码</option>
                                    </select>
                                </div>
                            </div>


                            <div class="form-group">
                                <label for="useType" class="col-sm-3 control-label"><span class="red">*</span> 优惠券使用类型：</label>
                                <div class="col-sm-3">
                                    <select class="form-control" name="useType" >
                                        <option value="0">请选择</option>
                                        <option value="1" <!--{if isset($coupon_array) && $coupon_array.useType == 1}--> selected="selected" <!--{/if}--> >券</option>
                                        <option value="2" <!--{if isset($coupon_array) && $coupon_array.useType == 2}--> selected="selected" <!--{/if}--> >红包</option>
                                    </select>
                                </div>
                            </div>



                            <div class="form-group">
                                <label for="limitMoney" class="col-sm-3 control-label"><span class="red">*</span> 订单限额：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="limitMoney" name="limitMoney" placeholder="请输入订单限额【必填项】" value="<!--{$coupon_array.limitMoney|default:''}-->">
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="amount" class="col-sm-3 control-label"><span class="red">*</span> 优惠券抵扣金额：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" id="amount" name="amount" placeholder="请输入优惠券抵扣金额【必填项】" value="<!--{$coupon_array.amount|default:''}-->">
                                </div>
                            </div>

                            <div class="form-group"  id="totalNum">
                                <label for="totalNum" class="col-sm-3 control-label"><span class="red">*</span> 发放数目：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" name="totalNum" placeholder="请输入优惠券发放数目【必填项】" value="<!--{$coupon_array.totalNum|default:''}-->">
                                </div>
                            </div>


                            <div class="form-group hide"  id="code_number" >
                                <label for="code_number" class="col-sm-3 control-label"><span class="red">*</span> 券号长度：</label>
                                <div class="col-sm-4">
                                    <select name="code_number" class="form-control" style="width: 100px;">
                                        <option value="8" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 8}-->selected<!--{/if}-->>8</option>
                                        <option value="9" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 9}-->selected<!--{/if}-->>9</option>
                                        <option value="10" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 10}-->selected<!--{/if}-->>10</option>
                                        <option value="11" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 11}-->selected<!--{/if}-->>11</option>
                                        <option value="12" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 12}-->selected<!--{/if}-->>12</option>
                                        <option value="13" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 13}-->selected<!--{/if}-->>13</option>
                                        <option value="14" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 14}-->selected<!--{/if}-->>14</option>
                                        <option value="15" <!--{if isset($coupon_array.code_number) && $coupon_array.code_number eq 15}-->selected<!--{/if}-->>15</option>
                                    </select>
                                </div>
                            </div>

                            <div class="form-group hide" id="wncode">
                                <label for="wncode" class="col-sm-3 control-label"><span class="red">*</span> 万能码：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" name="wncode" placeholder="请输入万能码【必填项】" value="<!--{$coupon_array.wncode|default:''}-->">
                                </div>
                            </div>



                            <div class="form-group hide" id="validity">
                                <label for="validity" class="col-sm-3 control-label"><span class="red">*</span> 有效期：</label>
                                <div class="col-sm-4">
                                    <input type="text" class="form-control" name="validity" placeholder="请输入有效期【必填项】" value="<!--{$coupon_array.validity|default:''}-->">
                                </div>
                            </div>

                            <div class="form-group ">
                                <label class="col-sm-3 control-label"><span class="red">*</span> 城市：</label>
                                <div class="col-sm-4">

                                    <!--{foreach from=$city_array item=city}-->
                                    <div class="fl" style="margin: 0px 10px 10px 0px;">
                                        <input type="checkbox" class="square-radio" id="city-checkbox-<!--{$city.id}-->" name="cityID" value="<!--{$city.id}-->" <!--{if isset($coupon_array.cityID) && $city.id|in_array:$coupon_array.cityID}--> checked="checked" <!--{/if}--> >
                                        <label for="city-checkbox-<!--{$city.id}-->"><!--{$city.name}--></label>
                                    </div>
                                    <!--{/foreach}-->
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="departmentID" class="col-sm-3 control-label"> 优惠券领用部门：</label>
                                <div class="col-sm-9">
                                    <!--<select class="form-control" id="departmentID" name="departmentID" style="width: 300px">
                                        <li>请选择</li>
                                        <!--$department_select_html}
                                    </select>-->
                                    <div class="search_option">
                                        <div class="search_option_drop" >
                                            <span id="searchOption" style="display:block;line-height:30px;">
                                                <!--{if isset( $coupon_array ) && !empty($coupon_array.departmentName)}-->
                                                    <!--{$coupon_array.departmentName}-->
                                                <!--{else}-->
                                                    请选择
                                                <!--{/if}-->
                                                <font class="imageSite"><img src="/quanliadmin/docroot/images/comm/selects.jpg"></font>
                                            </span>
                                        </div>

                                        <ul class="search_option_panel"  id="searchPanel" style="display: none;z-index: 100">
                                            <li class="childSelectImg" departmentID="0" style="margin-top:5px;margin-bottom:5px;margin-left: 0px;font-size: 13px;" >请选择</li>
                                        <!--{$department_select_html}-->
                                        </ul>

                                        <input type="hidden" value="0" id="hdnSearchType" name="hdnSearchType">
                                        <input type="hidden" value="" id="departmentID" name="departmentID">
                                        <input type="hidden" value="" id="departmentName" name="departmentName">
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="couponType" class="col-sm-3 control-label">选择商品筛选规则：</label>
                                <div class="col-sm-3">
                                    <select class="form-control" name="goodsSelectorID">
                                        <option value="0">请选择</option>
                                        <!--{if isset($goods_select)}-->
                                        <!--{foreach from=$goods_select item=goods}-->
                                        <option value="<!--{$goods->selectID}-->" <!--{if isset($coupon_array) && $coupon_array.goodsSelector == $goods->selectID}--> selected="selected" <!--{/if}--> ><!--{$goods->selectName}--></option>
                                        <!--{/foreach}-->
                                        <!--{/if}-->
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="couponType" class="col-sm-3 control-label">选择用户筛选规则：</label>
                                <div class="col-sm-3">
                                    <select class="form-control" name="userSelectorID">
                                        <option value="0">请选择</option>
                                        <!--{if isset($user_select)}-->
                                        <!--{foreach from=$user_select item=user}-->
                                        <option value="<!--{$user->selectID}-->" <!--{if isset($coupon_array) && $coupon_array.userSelector == $user->selectID}--> selected="selected" <!--{/if}-->  ><!--{$user->selectDisplayName}--></option>
                                        <!--{/foreach}-->
                                        <!--{/if}-->
                                    </select>
                                </div>
                            </div>

                            <div class="form-group">
                                <label for="validity" class="col-sm-3 control-label">使用描述：</label>
                                <div class="col-sm-8">
                                    <textarea class="form-control" name="description" id="description" rows="3" placeholder="请输入使用描述"><!--{$coupon_array.description|default:''}--></textarea>
                                </div>
                            </div>


                        </div>

                    </form>
                </div>
					
			</div>
			
		</div>
		
		<div class="popup-footer" style="padding-bottom: 20px;">
	        <div class="rectbox tac">
	            <input type="button" class="btn btn-success" onclick="coupon.check();" value="保存" />&nbsp;&nbsp;&nbsp;
	            <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
	        </div>
	    </div>

	</div>


<script type="text/javascript">

    //编辑时给部门赋值
    $(document).ready(function(){
        <!--{if isset( $coupon_array )}-->
            //$('#searchOption').text('<!--$coupon_array.departmentName}-->');
            $('#departmentID').val('<!--{$coupon_array.departmentID}-->');
            $('#departmentName').val('<!--{$coupon_array.departmentName}-->');
        <!--{/if}-->

        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });
    });

    $('#startDate').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,});
    $('#endDate').datetimepicker({timeFormat:'HH:mm:ss',showHour: true,showMinute: true,showSecond: true,hour: 23,minute: 59,second:59,});

    $("#couponName").focus();

    var coupon = {

        dt: "",

        /**
         * 优惠券发放方式变更
         */
        typeChange: function() {

            var t = $("select[name='couponType']").val();

            if (t == 1) {

                $("#wncode").addClass("hide");
                $("#validity").addClass("hide");
                $("#totalNum").removeClass("hide");
                $("#code_number").removeClass("hide");

            } else if (t == 2) {

                $("#wncode").addClass("hide");
                $("#validity").removeClass("hide");
                $("#totalNum").removeClass("hide");
                $("#code_number").addClass("hide");

            } else if (t == 3) {

                $("#wncode").removeClass("hide");
                $("#validity").addClass("hide");
                $("#totalNum").addClass("hide");
                $("#code_number").addClass("hide");

            } else {

                $("#wncode").addClass("hide");
                $("#validity").addClass("hide");
                $("#totalNum").removeClass("hide");
                $("#code_number").addClass("hide");

            }

        },

        check: function() {

            this.dt = E.getFormValues("couponForm");

            var error_msg = "";

            if (E.isEmpty(this.dt.couponName)) {
                error_msg += "优惠券名不能为空<br />";
            }



            if (E.isEmpty(this.dt.startDate) || E.isEmpty(this.dt.endDate)) {
                error_msg += "优惠券使用日期不能为空<br />";
            } else if (!E.dateCompare(this.dt.startDate, this.dt.endDate)) {
                error_msg += "开始日期不能大于结束日期<br />";
            }

            if (this.dt.couponType == 0) {
                error_msg += "请选择优惠券发放方式<br />";
            }


            if (this.dt.useType == 0) {
                error_msg += "请选择优惠券使用方式<br />";
            }

            if (!E.isMoney(this.dt.limitMoney) || this.dt.limitMoney < 0) {
                error_msg += "订单限额不能为空且必须是大于等于0的数字<br />";
            }

            if (!E.isMoney(this.dt.amount) || this.dt.amount <= 0) {
                error_msg += "优惠券抵扣金额不能为空且必须是大于0的数字<br />";
            }

            if ((this.dt.limitMoney > 0) && (parseFloat(this.dt.amount) > parseFloat(this.dt.limitMoney))) {
                error_msg += "优惠券抵扣金额不能大于订单限额<br />";
            }

            if ( this.dt.couponType == 1 ){
                if (!E.isDigital(this.dt.code_number) || this.dt.code_number <= 0) {
                    error_msg += "券号长度不能为空且必须是大于0的整数<br />";
                }
            }

            if (this.dt.couponType == 1 || this.dt.couponType == 2) {
                if (!E.isDigital(this.dt.totalNum) || this.dt.totalNum <= 0) {
                    error_msg += "发放数目不能为空且必须是大于0的整数<br />";
                }
            }

            if (this.dt.couponType == 2) {

                if (!E.isDigital(this.dt.validity) || this.dt.validity <= 0) {
                    error_msg += "有效期不能为空且必须是大于0的整数<br />";
                }

            } else if (this.dt.couponType == 3) {

                if (E.isEmpty(this.dt.wncode)) {
                    error_msg += "万能码不能为空<br />";
                }

            }

            if ( E.isEmpty(this.dt.cityID[0]) && E.isEmpty(this.dt.cityID[1]) ) {

                error_msg += "城市必须选择<br />";

            }

           if (error_msg != "") {
                E.alert(error_msg);
            } else {
                if (this.dt.couponID == 0) {
                    E.confirm("您确认要新增优惠券吗？", "coupon.execute");
                } else {
                    E.confirm("您确认要修改优惠券吗？", "coupon.execute");
                }
            }

        },

        execute: function() {

            if (this.dt.couponID == 0) {
                E.loadding.open("正在新增优惠券，请稍候...");
            } else {
                E.loadding.open("正在修改优惠券，请稍候...");
            }

            E.ajax_post({
                url: "/invokingAjax-backend/promotion/coupon.ajax?operFlg=2&departmentName="+this.departmentName,
                data: this.dt,
                call: "coupon.result"
            });

        },

        result: function( o ) {

            E.loadding.close();

            if (o.code == 200) {
                E.opener("coupon.load")
                if (this.dt.couponID == 0) {
                    E.alert(o.message, 2, "E.refresh");
                } else {
                    E.alert(o.message, 2, "E.closeWindows");
                }
            } else {
                E.alert(o.message);
            }

        }

    };

    <!--{if isset($coupon_array)}-->
    coupon.typeChange();
    <!--{/if}-->

</script>
</body>
</html>